package view;
import controller.main;
import multithread.CalculMultiThread;
import simplethread.CalculSimpleThread;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.ExecutionException;
import javax.swing.*;

public  class InterfaceGUI extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
////Variables declaration - do not modify
	private JComboBox ArgStock = new JComboBox();
    private JComboBox ArgStrike = new JComboBox();
    private JComboBox ArgDiscount = new JComboBox();
    private JComboBox ArgMature = new JComboBox();
    private JComboBox ArgCost = new JComboBox();
    private JComboBox ArgVolatile = new JComboBox();
    private JComboBox ArgSteps = new JComboBox();
    private JComboBox ArgSimu = new JComboBox();
    
    private JLabel Parameters = new JLabel("Parameters");
    private JLabel LabelargStock = new JLabel("StockPrice of the day");
    private JLabel LabelargStrike = new JLabel("Strike Price");
    private JLabel LabelargDiscount = new JLabel("Discount rate");
    private JLabel LabelargMature = new JLabel("Time to maturity");
    private JLabel LabelargCost = new JLabel("Cost of carry rate");
    private JLabel LabelargVolatile = new JLabel("Volatility");
    private JLabel LabelargSteps = new JLabel("Number of steps");
    private JLabel LabelargSimu = new JLabel("Number of simulations");
    private JLabel SimpleResult = new JLabel("Simple Threading Process Result : ");
    private JLabel TimeSimple = new JLabel("Simple Threading Execution Time : ");
    private JLabel MultiResult = new JLabel("Multi Threading Process Result : ");
    private JLabel TimeMulti = new JLabel("Multi Threading Execution Time : ");
    private JLabel Gain = new JLabel("Time saved : ");
    private JLabel Result = new JLabel("Results");
    
    private JButton Quit = new JButton("Exit software");
    private JButton Start = new JButton("Begin operation");
    
    public JProgressBar SimpleProgressBar = new JProgressBar(); 
    public JProgressBar MultiProgressBar = new JProgressBar();
    
    public JPanel ghost = new JPanel();

	private String[][] tab;

	public InterfaceGUI(String[][] recuperation_information) {		
		this.tab=recuperation_information;
		
		setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        SpringLayout layout = new SpringLayout();
        
        layout.putConstraint(SpringLayout.NORTH, Parameters,
                20,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, Parameters,
                170,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgStock,
                50,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgStock,
                200,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgStrike,
                100,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgStrike,
                200,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgDiscount,
                150,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgDiscount,
                210,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgMature,
                200,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgMature,
                203,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgCost,
                250,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgCost,
                210,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgVolatile,
                300,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgVolatile,
                203,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgSteps,
                350,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgSteps,
                205,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, ArgSimu,
                400,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, ArgSimu,
                225,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargStock,
                50,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargStock,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargStrike,
                100,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargStrike,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargDiscount,
                150,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargDiscount,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargMature,
                200,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargMature,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargCost,
                250,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargCost,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargVolatile,
                300,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargVolatile,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargSteps,
                350,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargSteps,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, LabelargSimu,
                400,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, LabelargSimu,
                130,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, Start,
                50,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, Start,
                450,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, Quit,
                400,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.EAST, Quit,
                450,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, SimpleProgressBar,
                100,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, SimpleProgressBar,
                250,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, SimpleResult,
                120,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, SimpleResult,
                250,
                SpringLayout.WEST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, TimeSimple,
                140,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, TimeSimple,
                250,
                SpringLayout.WEST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, MultiProgressBar,
                200,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, MultiProgressBar,
                250,
                SpringLayout.EAST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, MultiResult,
                220,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, MultiResult,
                250,
                SpringLayout.WEST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, TimeMulti,
                240,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, TimeMulti,
                250,
                SpringLayout.WEST, ghost);
        
        layout.putConstraint(SpringLayout.NORTH, Gain,
                340,
                SpringLayout.NORTH, ghost);
       
        layout.putConstraint(SpringLayout.WEST, Gain,
                250,
                SpringLayout.WEST, ghost);
		
        this.add(Parameters);
		this.add(ArgStock);
		this.add(ArgStrike);
		this.add(ArgDiscount);
		this.add(ArgMature);
		this.add(ArgCost);
		this.add(ArgVolatile);
		this.add(ArgSteps);
		this.add(ArgSimu);
		
		this.add(LabelargStock);
		this.add(LabelargStrike);
		this.add(LabelargDiscount);
		this.add(LabelargMature);
		this.add(LabelargCost);
		this.add(LabelargVolatile);
		this.add(LabelargSteps);
		this.add(LabelargSimu);
		
		this.add(Start);
		this.add(Quit);
		
		this.add(SimpleProgressBar);
		this.add(MultiProgressBar);
		this.add(SimpleResult);
		this.add(MultiResult);
		this.add(TimeSimple);
		this.add(TimeMulti);
		this.add(Gain);
		
        this.setLayout(layout);
        this.setVisible(true);
		
		this.Quit.addActionListener(this);
		this.Start.addActionListener(this);
		
		for(int i=0;i<this.tab.length;i++)
		{
			for(int j=0;j<this.tab[i].length;j++)
			{
				switch(i)
				{
				case 0 :ArgStock.addItem(this.tab[i][j]);
				break;
				case 1 :ArgStrike.addItem(this.tab[i][j]);
				break;
				case 2 :ArgDiscount.addItem(this.tab[i][j]);
				break;
				case 3 :ArgMature.addItem(this.tab[i][j]);
				break;
				case 4 :ArgCost.addItem(this.tab[i][j]);
				break;
				case 5 :ArgVolatile.addItem(this.tab[i][j]);
				break;
				case 6 :ArgSteps.addItem(this.tab[i][j]);
				break;
				case 7 :ArgSimu.addItem(this.tab[i][j]);
				break;
				}
			}
		}
        
        
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource()==Quit)
		{
		System.exit(0);
		}	
		if(e.getSource()==Start)
		{
	
			 Runnable runner = new Runnable()
			 {
			 public void run() {
				 double Start = System.currentTimeMillis();
				 int v = new Integer ((String) ArgSimu.getSelectedItem());

				 main.GUI.MultiProgressBar.setMaximum(v);
				 main.GUI.SimpleProgressBar.setMaximum(v);
				 main.GUI.MultiProgressBar.setValue(0);
				 main.GUI.SimpleProgressBar.setValue(0);
				 	
					CalculSimpleThread Cal = new CalculSimpleThread(
							  "chris", //String CallPutFlag
							  new Double((String)ArgStock.getSelectedItem()),
							  new Double((String)ArgStrike.getSelectedItem()),
							  new Double((String)ArgMature.getSelectedItem()),
							  new Double((String)ArgDiscount.getSelectedItem()),
							  new Double((String)ArgCost.getSelectedItem()),
							  new Double((String)ArgVolatile.getSelectedItem()),
							  new Integer((String)ArgSteps.getSelectedItem()),
							  new Integer((String)ArgSimu.getSelectedItem()));
					Cal.CalculOneThread();
					SimpleResult.setText("Simple Threading Process Result : "+Cal.getResult());
					Double ExecTime = System.currentTimeMillis()-Start;
					
					TimeSimple.setText("Simple Threading Execution Time : "+ ExecTime.toString() +" ms");
					
					
					
					 try {
							CalculMultiThread Cal2 = new CalculMultiThread(
									  "chris", //String CallPutFlag
									  new Double((String) ArgStock.getSelectedItem()),
									  new Double((String) ArgStrike.getSelectedItem()),
									  new Double((String) ArgMature.getSelectedItem()),
									  new Double((String) ArgDiscount.getSelectedItem()),
									  new Double((String) ArgCost.getSelectedItem()),
									  new Double((String) ArgVolatile.getSelectedItem()),
									  new Integer((String) ArgSteps.getSelectedItem()), 
									  new Integer((String) ArgSimu.getSelectedItem()));
							MultiResult.setText("Multi Threading Process Result : "+Cal2.getResutl());
							TimeMulti.setText("Multi Threading Execution Time : "+Cal2.getTime()+ " ms");
							
							Gain.setText("Time Saved : "+(ExecTime-Cal2.getTime())+"ms");
							
						} catch (NumberFormatException e2) {
							e2.printStackTrace();
						} catch (InterruptedException e2) {
							e2.printStackTrace();
						}

				 catch (ExecutionException e1) {
							e1.printStackTrace();
						}
			 
			 }
			 };
			 Thread thread = new Thread(runner, "loop");
			 thread.start();				
				}
	}

}
